package com.healthcarex.hip.saas.common.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;

/**
 * @author chenpan
 * @ClassName: BaseModel
 * description:
 * @date 2018-04-02 13:44
 */
@Table
@Data
@Slf4j
public class BaseModel implements Serializable {

    public final static String BASE_SEQ = "SEQ_GLOBAL";

    @Id
    /*@KeySql(sql = "select SEQ_GLOBAL.nextval from dual", order = ORDER.BEFORE)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "globalSeq")
    @SequenceGenerator(name = "globalSeq", sequenceName = "SEQ_GLOBAL", allocationSize = 1)*/
    @Column(name = "ID")
    @ApiModelProperty(value = "主键ID")
    private Integer id;

    @ApiModelProperty(value = "创建人", hidden = true)
    @Column(name = "CREATE_BY")
    private String createBy;

    @ApiModelProperty(value = "创建时间", hidden = true)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "CREATE_TIME")
    private Date createTime;

    @ApiModelProperty(value = "更改次数", hidden = true)
    @Column(name = "UPDATE_COUNT")
    private Integer updateCount;

    @ApiModelProperty(value = "更新人", hidden = true)
    @Column(name = "UPDATE_BY")
    private String updateBy;

    @ApiModelProperty(value = "更新时间", hidden = true)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "UPDATE_TIME")
    private Date updateTime;

    @ApiModelProperty(value = "删除标识", hidden = true)
    @Column(name = "DELETE_FLAG")
    private String deleteFlag;

    @ApiModelProperty(value = "删除时间", hidden = true)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "DELETE_TIME")
    private Date deleteTime;

    @ApiModelProperty(value = "排序字段")
    @Transient
    private String sortBy;


    public Object getValueByName(String fieldName) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = this.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(this, new Object[]{});
            return value;
        } catch (Exception e) {
            log.error(fieldName + "属性不存在");
            return null;
        }
    }
}
